home *** CD-ROM | disk | FTP | other *** search
/ PC go! 2014 January / PCgo_CD_14_01.iso / nw.pak / Unnamed File 000847.txt < prev    next >
Encoding:
Text File  |  2012-12-14  |  5.6 KB  |  175 lines

  1. // Copyright (c) 2012 Intel Corp
  2. // Copyright (c) 2012 The Chromium Authors
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. //  in the Software without restriction, including without limitation the rights
  7. //  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell co
  8. // pies of the Software, and to permit persons to whom the Software is furnished
  9. //  to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in al
  12. // l copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IM
  15. // PLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNES
  16. // S FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS
  17. //  OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WH
  18. // ETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  19. //  CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  20.  
  21. var v8_util = process.binding('v8_util');
  22.  
  23. function MenuItem(option) {
  24.   if (typeof option != 'object')
  25.     throw new String('Invalid option.');
  26.  
  27.   if (!option.hasOwnProperty('type'))
  28.     option.type = 'normal';
  29.  
  30.   if (option.type != 'normal' &&
  31.       option.type != 'checkbox' &&
  32.       option.type != 'separator')
  33.     throw new String('Invalid MenuItem type: ' + option.type);
  34.  
  35.   if (option.type == 'normal' || option.type == 'checkbox') {
  36.     if (option.type == 'checkbox')
  37.       option.checked = Boolean(option.checked);
  38.  
  39.     if (!option.hasOwnProperty('label'))
  40.       throw new String('A normal MenuItem must have a label');
  41.     else
  42.       option.label = String(option.label);
  43.  
  44.     if (option.hasOwnProperty('icon')) {
  45.       option.shadowIcon = String(option.icon);
  46.       option.icon = nw.getAbsolutePath(option.icon);
  47.     }
  48.  
  49.     if (option.hasOwnProperty('tooltip'))
  50.       option.tooltip = String(option.tooltip);
  51.  
  52.     if (option.hasOwnProperty('enabled'))
  53.       option.enabled = Boolean(option.enabled);
  54.  
  55.     if (option.hasOwnProperty('submenu')) {
  56.       if (v8_util.getConstructorName(option.submenu) != 'Menu')
  57.         throw new String("'submenu' must be a valid Menu");
  58.  
  59.       // Transfer only object id
  60.       v8_util.setHiddenValue(this, 'submenu', option.submenu);
  61.       option.submenu = option.submenu.id;
  62.     }
  63.  
  64.     if (option.hasOwnProperty('click')) {
  65.       if (typeof option.click != 'function')
  66.         throw new String("'click' must be a valid Function");
  67.       else
  68.         this.click = option.click;
  69.     }
  70.   } else if (option.type == 'separator') {
  71.     option = {
  72.       type: 'separator'
  73.     };
  74.   }
  75.  
  76.   v8_util.setHiddenValue(this, 'option', option);
  77.   nw.allocateObject(this, option);
  78.  
  79.   // All properties must be set after initialization.
  80.   if (!option.hasOwnProperty('icon'))
  81.     option.shadowIcon = '';
  82.   if (!option.hasOwnProperty('tooltip'))
  83.     option.tooltip = '';
  84.   if (!option.hasOwnProperty('enabled'))
  85.     option.enabled = true;
  86. }
  87. require('util').inherits(MenuItem, exports.Base);
  88.  
  89. MenuItem.prototype.__defineGetter__('type', function() {
  90.   return this.handleGetter('type');
  91. });
  92.  
  93. MenuItem.prototype.__defineSetter__('type', function() {
  94.   throw new String("'type' is immutable at runtime");
  95. });
  96.  
  97. MenuItem.prototype.__defineGetter__('label', function() {
  98.   return this.handleGetter('label');
  99. });
  100.  
  101. MenuItem.prototype.__defineSetter__('label', function(val) {
  102.   this.handleSetter('label', 'SetLabel', String, val);
  103. });
  104.  
  105. MenuItem.prototype.__defineGetter__('icon', function() {
  106.   return this.handleGetter('shadowIcon');
  107. });
  108.  
  109. MenuItem.prototype.__defineSetter__('icon', function(val) {
  110.   v8_util.getHiddenValue(this, 'option').shadowIcon = String(val);
  111.   var real_path = val == '' ? '' : nw.getAbsolutePath(val);
  112.   this.handleSetter('icon', 'SetIcon', String, real_path);
  113. });
  114.  
  115. MenuItem.prototype.__defineGetter__('tooltip', function() {
  116.   return this.handleGetter('tooltip');
  117. });
  118.  
  119. MenuItem.prototype.__defineSetter__('tooltip', function(val) {
  120.   this.handleSetter('tooltip', 'SetTooltip', String, val);
  121. });
  122.  
  123. MenuItem.prototype.__defineGetter__('checked', function() {
  124.   if (this.type != 'checkbox')
  125.     return undefined;
  126.     
  127.   return this.handleGetter('checked');
  128. });
  129.  
  130. MenuItem.prototype.__defineSetter__('checked', function(val) {
  131.   if (this.type != 'checkbox')
  132.     throw new String("'checked' property is only available for checkbox");
  133.     
  134.   this.handleSetter('checked', 'SetChecked', Boolean, val);
  135. });
  136.  
  137. MenuItem.prototype.__defineGetter__('enabled', function() {
  138.   return this.handleGetter('enabled');
  139. });
  140.  
  141. MenuItem.prototype.__defineSetter__('enabled', function(val) {
  142.   this.handleSetter('enabled', 'SetEnabled', Boolean, val);
  143. });
  144.  
  145. MenuItem.prototype.__defineGetter__('submenu', function() {
  146.   return v8_util.getHiddenValue(this, 'submenu');
  147. });
  148.  
  149. MenuItem.prototype.__defineSetter__('submenu', function(val) {
  150.   if (v8_util.getConstructorName(val) != 'Menu')
  151.     throw new String("'submenu' property requries a valid Menu");
  152.  
  153.   v8_util.setHiddenValue(this, 'submenu', val);
  154.   nw.callObjectMethod(this, 'SetMenu', [ val.id ]);
  155. });
  156.  
  157. MenuItem.prototype.handleEvent = function(ev) {
  158.   if (ev == 'click') {
  159.     // Automatically flag the 'checked' property.
  160.     if (this.type == 'checkbox') {
  161.       var option = v8_util.getHiddenValue(this, 'option');
  162.       option.checked = !option.checked;
  163.     }
  164.  
  165.     // Emit click handler
  166.     if (typeof this.click == 'function')
  167.       this.click();
  168.   }
  169.  
  170.   // Emit generate event handler
  171.   exports.Base.prototype.handleEvent.apply(this, arguments);
  172. }
  173.  
  174. exports.MenuItem = MenuItem;
  175.